package com.wing.cabinet.controller.app;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.response.KnifeUserVO;
import com.wing.cabinet.model.response.UseInventoryProductVO;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.cabinet.model.entity.UseInventoryUser;
import com.wing.cabinet.model.response.UseInventoryUserVO;
import com.wing.cabinet.service.UseInventoryUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.cabinet.model.request.UseInventoryUserForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-用户产品在用库存管理", tags = "用户产品在用库存管理")
@RestController("appUseInventoryUserController")
@RequestMapping("cabinet/app/useInventoryUser")
@Slf4j
public class UseInventoryUserController {

    @Autowired
    private UseInventoryUserService useInventoryUserService;
    @Autowired
    private LocaleMessage localeMessage;

    @GetMapping()
    public PageResult<UseInventoryUserVO> list(
            @RequestParam(name = "page", value = "第几页", defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit", value = "每页多少条", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort", value="排序字段名", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", required = false) String order
    ) {
        Page<UseInventoryUserVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        Page<UseInventoryUserVO> list = useInventoryUserService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/storage/listIn")
    @ApiOperation(value = "存刀列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    public PageResult<UseInventoryUserVO> storageListIn(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "asc") String order
            ,String deviceCode
            ,Long productCategoryId
            ,Long productTypeId
            ,String cabCodes
            ,String skuName
            ,String brandName
            ,String typeName
            ,String name
            ,String orderCode
            ,String labelCode
    ) {
        Page<UseInventoryProductVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("queryFlag","useInventoryProduct4App");
        params.put("deviceCode",deviceCode);
        params.put("productCategoryId",productCategoryId);
        params.put("productTypeId",productTypeId);
        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("skuName",skuName);
        params.put("brandName",brandName);
        params.put("typeName",typeName);
        params.put("name",name);
        params.put("deviceCode",deviceCode);
        params.put("orderCode",orderCode);
        params.put("labelCode",labelCode);
        log.info("storage user listIn:{}", JSON.toJSONString(params));
        Page<UseInventoryUserVO> list = useInventoryUserService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @GetMapping("/repair/listIn")
    @ApiOperation(value = "存维修刀列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    public PageResult<UseInventoryUserVO> repairtListIn(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "asc") String order
            ,String deviceCode
            ,String cabCodes
            ,Long productCategoryId
            ,Long productTypeId
            ,String skuName
            ,String brandName
            ,String typeName
            ,String name
            ,String orderCode
            ,String labelCode
    ) {
        Page<UseInventoryProductVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("deviceCode",deviceCode);
        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("skuName",skuName);
        params.put("brandName",brandName);
        params.put("typeName",typeName);
        params.put("name",name);
        params.put("deviceCode",deviceCode);
        params.put("productCategoryId",productCategoryId);
        params.put("productTypeId",productTypeId);
        params.put("queryFlag","useInventoryProduct4App");
        params.put("orderCode",orderCode);
        params.put("labelCode",labelCode);
        log.info("repair user listIn:{}", JSON.toJSONString(params));
        Page<UseInventoryUserVO> list = useInventoryUserService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/recycle/listIn")
    @ApiOperation(value = "存报废刀列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "name", value = "名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    public PageResult<UseInventoryUserVO> recycleListIn(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "asc") String order
            ,String deviceCode
            ,String cabCodes
            ,Long productCategoryId
            ,Long productTypeId
            ,String skuName
            ,String brandName
            ,String typeName
            ,String name
            ,String orderCode
            ,String labelCode
    ) {
        Page<UseInventoryProductVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("queryFlag","useInventoryProduct4App");
        params.put("deviceCode",deviceCode);
        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("skuName",skuName);
        params.put("brandName",brandName);
        params.put("typeName",typeName);
        params.put("name",name);
        params.put("deviceCode",deviceCode);
        params.put("productCategoryId",productCategoryId);
        params.put("productTypeId",productTypeId);
        params.put("orderCode",orderCode);
        params.put("labelCode",labelCode);
        log.info("recycle user listIn:{}", JSON.toJSONString(params));
        Page<UseInventoryUserVO> list = useInventoryUserService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long useInventoryUserId) {
        UseInventoryUserVO useInventoryUserVO = useInventoryUserService.get4appById(useInventoryUserId);
        if (useInventoryUserVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), useInventoryUserVO);
    }

    @PostMapping()
    public JsonResult add(@Validated UseInventoryUserForm useInventoryUserForm) {
        if (useInventoryUserService.add(useInventoryUserForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    @ApiOperation(value = "查詢领/取刀人列表", notes = "搜索")
    @GetMapping("/getKnifeUsers")
    public JsonResult<List<KnifeUserVO>> getKnifeUsers(){
        return JsonResult.success(useInventoryUserService.getKnifeUsers());
    }


}
